/**
 * 
 */
package de.oszimtcc.googleapi;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.google.api.services.calendar.model.Event;
import de.oszimtcc.timetable.R;


/**
 * @author reuter
 *
 */
public class AppointmentAdapter extends ArrayAdapter<Event>
{
	private Context mContext;
	private List<Event> eventList = new ArrayList<Event>();
	
	public AppointmentAdapter(Context pContext, List<Event> pEventList) 
	{
		super(pContext, 0);
		mContext = pContext;
		this.eventList = pEventList;
	}
	
	@Override
	public int getCount() 
	{
		return (eventList != null ? eventList.size() : 0);
	}

	public Event getEvent(int pPosition) 
	{
		if(eventList == null || eventList.isEmpty() || pPosition > eventList.size() - 1) 
		{
			return null;
		}
		
		return eventList.get(pPosition);
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) 
	{
		View v = new View(mContext);
		if (convertView == null) 
		{
			LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.event, null);
            
		} 
		else 
		{
			v = convertView;
		}
		
		Event event = eventList.get(position);
		
		if(event != null)
		{
			TextView summary = (TextView) v.findViewById(R.id.event_eventSummary);
			summary.setText(event.getSummary());		
			v.setTag(event);
		}
		return v;
	}
	
	public List<Event> getData() 
	{
		return eventList;
	}

	public void addData(List<Event> pEventList) 
	{
		List<Event> tmp = pEventList;
		for(Event event : pEventList)
        {
	        if(!this.eventList.contains(event))
	        	tmp.add(event);
        }
		if(tmp.size() > 0)
		{
			this.eventList.addAll(tmp);
			this.notifyDataSetChanged();
		}
	}
	
	public void addData(Event pEvent) 
	{
		if(!this.eventList.contains(pEvent))
		{
			this.eventList.add(pEvent);
			this.notifyDataSetChanged();
		}
	}
}
